<template>
	<view class="content">
		<view class="">18</view>
		<view class="">
			<button class="login-btn" @click="getAuth">手机号一键登录</button>
			<button class="login-btn" @click="getPhone">手机号一键登录33</button>
			<br/><br/>
			<button class="login-btn" @click="wechatLogin">微信授权</button>
			
			<br/><br/>
			<view class="">以下是测试反馈数据</view>
			<view class="">1.getAuth == {{ data.auth }}</view>
			<view class="">2.checkLogin == {{ data.login }}</view>
			<view class="">3.getAliPreToken == {{ data.aliPreToken }}</view>
			<view class="">4.getPhone == {{ data.phone }}</view>
		</view>
	</view>


</template>

<script setup>
	import { wechatAuth } from '@/utils/common.js'
	import { PhoneNumberServer } from 'aliyun_numberauthsdk_web'; 
	import { getAliToken, getAliPhone } from '@/api/login.js'
	
	var phoneNumberServer = new PhoneNumberServer();
	const data = reactive({
		auth: '1',
		login:'1',
		aliPreToken:'1',
		phone:'1'
	})
	const getAuth = ()=>{
		data.auth = '2'
		getAliToken().then(res=>{
			data.auth = res.data.accessToken + '==' + res.data.jwtToken
			checkLogin(res.data.accessToken,res.data.jwtToken)
		})
	}
	const checkLogin = (toekn,jwtToken)=> {
		data.login = '2'
		phoneNumberServer.checkLoginAvailable({
			accessToken: toekn,
			jwtToken: jwtToken,
			success: function (res) {
				if (res.code === 600000) {
					data.login = "鉴权成功"
					getAliPreToken()
				}
			},
			error: function (res) {
				data.login = "登录失败，请关闭wifi,连接移动网络后重试"+ JSON.stringify(res)
			}
		});
	}
	const getAliPreToken = ()=> {
		data.aliPreToken = '2'
		phoneNumberServer.getLoginToken({
		    success: function (res) {
				data.aliPreToken = "getAliPreToken成功"+ JSON.stringify(res)
				// 成功回调，获取spToken
				getPhone(res.spToken);
		    },
		    // 失败回调
		    error: function (res) {
				data.aliPreToken = "getAliPreToken失败"+ JSON.stringify(res)
				// alert("getAliPreToken失败"+ JSON.stringify(res))
				// 提示用户关闭Wi-Fi或者尝试其他登录方案
			},
		    
		    // 授权页状态监听函数
		    watch: function (status, data) {},
		    
		    // 页面配置选项
		    authPageOption: {},
		  });
	}
	const getPhone = (ptoken)=> {
		alert("ptoken",ptoken)
		const mock = {
			token: 'eyJzcFRva2VuIj8384-48ac-ac5a-3b6a0456fc3e'
		}
		getAliPhone(mock).then(res=>{
			console.log(res)
			alert("2222=="+ JSON.stringify(res))
			// checkLogin(res.data.accessToken,res.data.jwtToken)
		})
		
	}
	
	
	
	
	
	
	// wechat login
	
	const wechatLogin = ()=>{
		wechatAuth('pages/wechatLogin')
	}
	
	
	
</script>

